//#include "StdAfx.h"
//#include "dllmanager.h"
#include <stdio.h>
#include <stdlib.h>
#include "cfort.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "linked_list.h"


#include "windows.h"

//__declspec(dllimport) void CLSL_INIT(int ITYP,char *ABCD, int NN,int NOUT,int IEXTR,int IER);
typedef void (*P_CLSL_NFIN_FUNC)(int IER);
typedef void (*P_CLSL_INIT_FUNC)(int ITYP,char *p,int NN, int NOUT, int IEXTR, int IER);
typedef void (*P_CLSL_SINI_FUNC)(int I, int IER);
typedef void (*P_CLSL_SELE_FUNC)(int NDDL, int *LM, int NDDL1, int *LM1, int IER);
typedef void (*P_CLSL_SFIN_FUNC)(int I, int IER);
typedef void (*P_CLSL_NINI_FUNC)(int IER);
typedef void (*P_CLSL_NRAZ_FUNC)(int IER);
typedef void (*P_CLSL_NELE_FUNC)(int NDDL, int *LM, int NDDL1, int *LM1, double *STH, int IER);
typedef void (*P_CLSL_NFAC_FUNC)(double DEPS, int IER);
typedef void (*P_CLSL_NSOL_FUNC)(double *R, double *P, int NIQ, double DPREC, int NITER, int IER);

static HMODULE m_pDLLhInstance = NULL;

int LoadDLL()
{
	if(!m_pDLLhInstance)
	{
		HMODULE hModule = LoadLibrary("SE_dlllink.dll");
		if (!hModule)
		{
			return 0;
		} 
		else
		{
			m_pDLLhInstance = hModule;
		}
	}
	return 1;
}

void clsl_nfin(int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NFIN_FUNC pFunc = (P_CLSL_NFIN_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NFIN");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}


void clsl_init01(int ITYP,int NN, int NOUT, int IEXTR, int IER)
{
//	CLSL_INIT(ITYP,"",NN, NOUT, IEXTR, IER);
	
	FILE *fp = fopen("12345.out","wt");

	fprintf(fp,"====>  Now, coming to CLSL_INIT -1\n"); 
	if(LoadDLL())
	{
		P_CLSL_INIT_FUNC pFunc = (P_CLSL_INIT_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_INIT");
		fprintf(fp,"====>  Now, coming to CLSL_INIT -2 \n");
		if (pFunc != NULL)
		{
            fprintf(fp,"====>  Now, coming to CLSL_INIT -3 \n"); 
			fprintf(fp,"ITYP = %d , NN = %d ,NOUT = %d\n",ITYP,NN,NOUT); 
			fclose(fp);
			pFunc(ITYP, "", NN, NOUT, IEXTR, IER);
            //fprintf(fp,"====>  coming back from CLSL_INIT \n");
		}
	}
	fclose(fp);
	
}


void clsl_sini(int I, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_SINI_FUNC pFunc = (P_CLSL_SINI_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_SINI");
		if (pFunc != NULL)
		{
			pFunc(I, IER);
		}
	}
}

void clsl_sele(int NDDL, int *LM, int NDDL1, int *LM1, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_SELE_FUNC pFunc = (P_CLSL_SELE_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_SELE");
		if (pFunc != NULL)
		{
			pFunc(NDDL, LM, NDDL1, LM1, IER);
		}
	}
}

void clsl_sfin(int I, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_SFIN_FUNC pFunc = (P_CLSL_SFIN_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_SFIN");
		if (pFunc != NULL)
		{
			pFunc(I, IER);
		}
	}
}

void clsl_nini(int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NINI_FUNC pFunc = (P_CLSL_NINI_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NINI");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}

void clsl_nraz(int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NRAZ_FUNC pFunc = (P_CLSL_NRAZ_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NRAZ");
		if (pFunc != NULL)
		{
			pFunc(IER);
		}
	}
}

void clsl_nele(int NDDL, int *LM, int NDDL1, int *LM1, double *STH, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NELE_FUNC pFunc = (P_CLSL_NELE_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NELE");
		if (pFunc != NULL)
		{
			pFunc(NDDL, LM, NDDL1, LM1, STH, IER);
		}
	}
}

void clsl_nfac(double DEPS, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NFAC_FUNC pFunc = (P_CLSL_NFAC_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NFAC");
		if (pFunc != NULL)
		{
			pFunc(DEPS, IER);
		}
	}
}

void clsl_nsol(double *R, double *P, int NIQ, double DPREC, int NITER, int IER)
{
	if(LoadDLL())
	{
		P_CLSL_NSOL_FUNC pFunc = (P_CLSL_NSOL_FUNC)GetProcAddress(m_pDLLhInstance, "CLSL_NSOL");
		if (pFunc != NULL)
		{
			pFunc(R, P, NIQ, DPREC, NITER,IER);
		}
	}
}


